<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../../jquery/jquery-1.11.1.js"></script>
</head>

<body>
    <p>
        <input type="file" id="file1">
    <div id="preview"></div>
    <button id="upload">上传</button>
    </p>
</body>
<script>
    $(function () {
        $('#upload').click(function () {
            if ($('#file1')[0].files.length === 0) return alert('请选择文件');
            let fd = new FormData();
            fd.append('avatar', $('#file1')[0].files[0]);
            $.ajax({
                method: 'POST',
                url: 'http://www.liulongbin.top:3006/api/upload/avatar',
                data: fd,
                contentType: false,
                processData: false,
                success: function(res) {
                    if (res && res.status === 200) {
                        $('#preview').html(`<img src="http://www.liulongbin.top:3006${res.url}" />`);
                    }
                }
            });
        });
    });
</script>

</html>